﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_DangKyHocPhan : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadThongTin();
            LoadDanhSach();
            LoadThongtinSinhVien();
        }
    }

    private void LoadThongtinSinhVien()
    {
        if (Request.QueryString["MSSV"] != null)
        {
            string mssv = Request.QueryString["MSSV"];
            var gvs = from p in data.tbl_SINHVIENs
                      where p.MsSinhVien == mssv
                      select p;
            foreach (tbl_SINHVIEN sv in gvs)
            {
                txtHoTenSV.Text = sv.TenSinhVien;
            }
            txtMSSV.Text = mssv;
            txtMSSV.Enabled = false;
        }
        else
        {
        }
    }

    private void LoadDanhSach()
    {
        GridView_DangKyHocPhan.DataSource = data.tbl_DANGKYLOPTCs;
        GridView_DangKyHocPhan.DataBind();
    }

    private void LoadThongTin()
    {
        ddlMonHoc.DataSource = data.tbl_MONHOCs;
        ddlMonHoc.DataTextField = "TenMonHoc";
        ddlMonHoc.DataValueField = "MsMonHoc";
        ddlMonHoc.DataBind();

        var query = from p in data.tbl_LOPTCs
                    where p.MsMonHoc == ddlMonHoc.SelectedValue.ToString()
                    select p;
        dllLopTC.DataSource = query;
        dllLopTC.DataTextField = "MsLopTC";
        dllLopTC.DataValueField = "MsLopTC";
        dllLopTC.DataBind();
    }
    protected void BtTimSV_Click(object sender, EventArgs e)
    {
        string mssv = txtMSSV.Text;
        var svs = from p in data.tbl_SINHVIENs
                  where p.MsSinhVien== mssv
                  select p;
        if (svs.Count() > 0)
        {
            foreach (tbl_SINHVIEN sv in svs)
            {
                txtHoTenSV.Text = sv.TenSinhVien;
            }
        }
        else
        {
            txtHoTenSV.Text = "";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Sinh viên không tồn tại!');", true);
        }
    }
    protected void ddlMonHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        var query = from p in data.tbl_LOPTCs
                    where p.MsMonHoc == ddlMonHoc.SelectedValue.ToString()
                    select p;
        dllLopTC.DataSource = query;
        dllLopTC.DataTextField = "MsLopTC";
        dllLopTC.DataValueField = "MsLopTC";
        dllLopTC.DataBind();
    }
    protected void GridView_DangKyHocPhan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_DangKyHocPhan.PageIndex = e.NewPageIndex;
        LoadDanhSach();
    }
    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        if (txtMSSV.Text == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa có thông tin sinh viên!');", true);
            return;
        }
        tbl_DANGKYLOPTC dangky = new tbl_DANGKYLOPTC();
        dangky.MsSinhVien = txtMSSV.Text;
        dangky.MsLopTC = dllLopTC.SelectedValue;

        data.tbl_DANGKYLOPTCs.InsertOnSubmit(dangky);
        data.SubmitChanges();
        LoadDanhSach();
    }
    protected void GridView_DangKyHocPhan_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "MyDelete")
        {
            string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
            string mssv = commandArgs[0];
            string msltc = commandArgs[1];

            var XoaHP = from p in data.tbl_DANGKYLOPTCs
                        where p.MsSinhVien == mssv && p.MsLopTC == msltc
                        select p;
            foreach (tbl_DANGKYLOPTC xoaHP in XoaHP)
            {
                data.tbl_DANGKYLOPTCs.DeleteOnSubmit(xoaHP);
            }
            data.SubmitChanges();
            LoadDanhSach();
        }
    }
    protected void GridView_DangKyHocPhan_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.setAttribute('bgColor', this.style.backgroundColor); this.style.backgroundColor = '#FF6600';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = this.getAttribute('bgColor');");
            }
        }
        catch { }
    }
}
